<?php
    $top_menu_1 = FALSE;
    $top_menu_2 = FALSE;
    $top_menu_3 = FALSE;
    $top_menu_4 = FALSE;
    $top_menu_5 = FALSE;
    $top_menu_6 = FALSE;
    $top_menu_7 = FALSE;
    include('header.php');
    include('sidebar2.php');
    
    if(isset($_SESSION['m_id'])){
        $m_id = $_SESSION['m_id'];
        $sql = "SELECT * FROM hs_member WHERE m_id = $m_id";
        $line = $db->queryUniqueObject($sql);
        
        $sql_relate = "SELECT * FROM hs_qoute WHERE m_id=$m_id ORDER BY q_dateadd DESC";
        
       // PAGE LIMIT
        if(isset($_GET['p'])){
            $page = $_GET['p'];
        }else{
            $page = 1;
        }
        
        $page_limit = 10;
        $grouplimit = 10;
        $start_row = (($page-1))*$page_limit;
        $sql_relate_nolimit = $sql_relate;
        $sql_relate .= " LIMIT $start_row,$page_limit";
        // PAGE LIMIT END
    
        // PAGE LIMIT
        $count_row = mysql_query($sql_relate_nolimit);
        $count_row2 = mysql_num_rows($count_row);
        $count_row3 = ceil($count_row2/$page_limit) ;
        
        if($count_row3>1)
        {
            if($count_row3<$grouplimit)
            {
                for($i = 1 ; $i<=$count_row3 ; $i++) {
                    if($i==$page){
                        $limitlink .=  "<span>".$i."</span>&nbsp;";
                    }else{
                        $limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
                    }
                }
            }
            else
            {
                $pagegroup = ceil($page/$grouplimit); // Find group of current page
                $allpagegroup = ceil($count_row3/$grouplimit); // Total page group
                if($pagegroup==1) // If First Group
                {
                    if($page>1)
                    {
                        $limitlink .= "<a href='?".$catselect."p=".($page-1).$pagecat."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;";
                    }
                    $i=($pagegroup*$grouplimit)-($grouplimit-1);
                    $j=($pagegroup*$grouplimit);
                    for($i ; $i<=$j ; $i++) {
                        if($i==$page){
                            $limitlink .= "<span>".$i."</span>&nbsp;";
                        }else{
                            $limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
                        }
                    }
                    $limitlink .= "...&nbsp;<a href='?".$catselect."p=".($page+1).$pagecat."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
                    $limitlink .= "<a href='?".$catselect."p=$count_row3".$pagecat."' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
                }
                else if($pagegroup==$allpagegroup) // If Last Group
                {
                    $limitlink = "<a href='?".$catselect."p=1".$pagecat."' title='First Pages'>&nbsp;<<&nbsp;first&nbsp;</a>&nbsp;";
                    $limitlink .= "<a href='?".$catselect."p=".($page-1).$pagecat."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;...&nbsp;";
                    $i=($pagegroup*$grouplimit)-($grouplimit-1);
                    $j=$count_row3;
                    for($i ; $i<=$j ; $i++) {
                        if($i==$page){
                            $limitlink .= "<span>".$i."</span>&nbsp;";
                        }else{
                            $limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
                        }
                    }
                    if($page<$count_row3)
                    {
                        $limitlink .= "&nbsp;<a href='?".$catselect."p=".($page+1).$pagecat."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
                        $limitlink .= "<a href='?".$catselect."p=$count_row3".$pagecat."' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
                    }
                }
                else
                {
                    $limitlink = "<a href='?".$catselect."p=1".$pagecat."' title='First Pages'>&nbsp;<<&nbsp;first&nbsp;</a>&nbsp;";
                    $limitlink .= "<a href='?".$catselect."p=".($page-1).$pagecat."' title='Previous Page'>&nbsp;<&nbsp;previous&nbsp;</a>&nbsp;...&nbsp;";
                    $i=($pagegroup*$grouplimit)-($grouplimit-1);
                    $j=($pagegroup*$grouplimit);
                    for($i ; $i<=$j ; $i++) {
                        if($i==$page){
                            $limitlink .=  "<span>".$i."</span>&nbsp;";
                        }else{
                            $limitlink .= "<a href='?".$catselect."p=$i".$pagecat."'>&nbsp;$i&nbsp;</a>&nbsp;";
                        }
                    }
                    $limitlink .= "...&nbsp;<a href='?".$catselect."p=".($page+1).$pagecat."' title='Next Page'>&nbsp;next&nbsp;>&nbsp;</a>&nbsp;";
                    $limitlink .= "<a href='?".$catselect."p=$count_row3".$pagecat."' title='Last Pages'>&nbsp;last&nbsp;>>&nbsp;</a>&nbsp;";
                }
            }
        }
        
        $rss2 = $db->query($sql_relate);
        if($db->numRows()){
            $check = TRUE;
            $productList = '';
            while ($line2 = $db->fetchNextObject($rss2)){
                $date = date('d/m/Y',$line2->q_dateadd);
                $q_id = $line2->q_id;
                
                $sql = "SELECT * FROM hs_qoute_product WHERE q_id=$q_id ORDER BY p_id";
                $rss = $db->query($sql);
                $productList = '';
                while ($line = $db->fetchNextObject($rss)){
                    $sql = "SELECT 	p_style_number , p_type , p_name , p_detail FROM hs_product WHERE p_id ={$line->p_id}";
                    $query_p = mysql_query($sql);
                    $result_p = mysql_fetch_assoc($query_p);
                    
                    $p_type = '&nbsp;';
                    $p_style_number = '&nbsp;';
                    $p_name = '&nbsp;';
                    $p_detail = '&nbsp;';
                    if(trim($result_p['p_type'])!=''){
                            $p_type = $result_p['p_type'];
                    }
                    if(trim($result_p['p_style_number'])!=''){
                            $p_style_number = $result_p['p_style_number'];
                    }
                    if(trim($result_p['p_name'])!=''){
                            $p_name = $result_p['p_name'];
                    }
                    if(trim($result_p['p_detail'])!=''){
                            $p_detail = $result_p['p_detail'];
                    }
                    
                        $productList .= <<<TBODY
                            <table class="qoute_product_list">
                                <tr>
                                    <td class="qoute_product_list_td_1">{$p_type}</td>
                                    <td class="qoute_product_list_td_2">{$p_style_number}</td>
                                    <td class="qoute_product_list_td_3">{$p_name}</td>
                                    <td class="qoute_product_list_td_4">{$p_detail}</td>
                                    <td class="qoute_product_list_td_5" class="center">{$line->quantity}</td>
                                </tr>
                            </table>
TBODY;
                }
                
                $qouteList .=<<<qouteList
                    <tr>
                        <td class="user_qoute_history_td user_qoute_history_td_f">{$date}</td>
                        <td class="user_qoute_history_td">{$productList}</td>
                    </tr>
qouteList;
            }
        }else{
             $check = FALSE;
        }
    }else{
         $check = FALSE;
    }
?>
            <div id="main_content" class="register_content">
                <img src="images/user_qoute_history_title.jpg" alt="Qoute History" id="user_qoute_history_title" />
                
                <div id="user_qoute_history" class="<?php if(!isset($_SESSION['m_id'])){ echo 'display_none'; } ?>">
                    <?php if($check): ?>
                    <table class ="user_qoute_history">
                        <?php echo $qouteList; ?>
                    </table>
                    
                    <div id="product_list_ctrl">
                        <?php
                            echo $limitlink;
                        ?>
                    </div>
                    
                    <?php else: ?>
                    <div id="coupon-detail">
                        <!--No Data-->
                    </div>
                    
                    <?php endif; ?>
                </div>
                
                <div id="success_msg2" class="<?php if(isset($_SESSION['m_id'])){ echo 'display_none'; } ?>">
                <?php
                    echo $status_msg['must_login'];
                ?>
                </div>
            </div>
            <div class="clear">&nbsp;</div>
<?php
    include('footer.php');
?>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/jquery.scrollTo-min.js"></script>
<script>
    $(function() {
        $("#regis_frm").validate({
            rules: {
                f_name : "required",
                l_name : "required",
                c_name : "required",
                email: {
                    required: true,
                    email: true
                },
                phone: "required",
                address: "required",
                city: "required",
                post_code: {
                    required: true,
                    number:true
                },
                country: "required",
                know: "required",
                o_password: "required",
                c_password: {
                    equalTo: "#n_password"
                }
            },
            messages: {
                f_name: "Please enter your firstname",
                l_name: "Please enter your lastname",
                c_name: "Please enter your company name",
                email: {
                    required: "Please enter an email address",
                    email: "Please enter a valid email address"
                },
                phone: "Please enter your phone number",
                address: "Please enter your address",
                city: "Please enter your city",
                city: "Please enter your city",
                post_code: {
                    required: "Please enter a postcode",
                    email: "Please enter a valid postcode"
                },
                country: "Please enter your country",
                know: "Please enter this field",
                o_password: "Please enter your password",
                c_password: {
                    equalTo: "Please enter the same password as above"
                }
            }
        });
        
        $('#register_submit_bt').click(function(){
            if($("#regis_frm").valid()){
                $("#register_submit_bt").css('display','none');
                $("#ajax_loader").css('display','block');
                var str = $("#regis_frm").serialize();
                $.post("function/update-user-info.php", str ,function(data){
                    if(trim(data.status)=='success'){
                        alert(data.msg);
                        $("#register_submit_bt").css('display','block');
                        $("#ajax_loader").css('display','none');
                    }else{
                        alert(data.msg);
                        $("#register_submit_bt").css('display','block');
                        $("#ajax_loader").css('display','none');
                        $("#o_password").focus();
                    }
                },'json');
            }else{
                $.scrollTo( '#regis_frm' , 1000);
            }
        });
    });
</script>